<template>
  <div>
    <Hearder />
    <el-card>
      <el-row :gutter="20">
        <el-col :span="10"
          ><el-input placeholder="请输入内容" v-model="from.query" clearable @clear='getList' >
            <el-button slot="append" icon="el-icon-search" @click="getList" ></el-button>
          </el-input>
        </el-col>
        <el-col :span="5">
          <el-button type="primary" @click="jump">添加</el-button>
        </el-col>
      </el-row>
      <el-table :data="goodList" border stripe>
        <el-table-column type="expand"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="580">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格" width="80">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="80">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间"> </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
       
            <el-button type="primary" icon="el-icon-edit" size="mini"
              >编辑</el-button
            >
            <el-button type="success" icon="el-icon-delete" size="mini" @click="deleteList(scope.row.goods_id)"
              >删除</el-button
            >
          
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="from.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="from.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totol"
        background
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      from: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      totol: 0,
      goodList: [],
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    jump(){
      this.$router.push('/goods/add')
    },
   async deleteList(id){
     console.log(id);
       const data=await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err=>err)
     if (data !== "confirm") return this.$Message.info("您已经成功取消");
         const {data:res}=await this.$http.delete(`goods/${id}`).catch(err=>err)
             this.getList();

    },
    handleSizeChange(val) {
      this.from.pagesize=val
      this.getList();
    },
    handleCurrentChange(val) {
     this.from.pagenum=val
      this.getList();
    },
    async getList() {
      const { data: res } = await this.$http
        .get("goods", { params: this.from })
        .catch((err) => err);
      this.goodList = res.data.goods;
        this.totol = res.data.total;
      res.data.goods.map(
        (item) => (item.add_time = this.$moment(item.add_time).format("LLLL"))
      );
      console.log(this.totol);
    
    },
  },
};
</script>

<style scoped>
.el-table {
  font-size: 12px;
}
</style>
